package com.megaspacevr.controller;

import com.megaspacevr.entity.Result;
import com.megaspacevr.entity.TimeSlots;
import com.megaspacevr.service.TimeSlotsService;
import com.megaspacevr.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 时间段表-30分钟小场次 前端控制器
 * </p>
 *
 * @author yezi
 * @since 2025-06-17
 */
@Slf4j
@RestController
@Validated
@RequestMapping("/timeSlots")
@Tag(name = "时间段管理", description = "VR展馆时间段相关接口")
@CrossOrigin(origins = "*")
public class TimeSlotsController {

    @Autowired
    private TimeSlotsService timeSlotsService;

    @GetMapping("/by-date")
    @Operation(summary = "根据日期获取时间段列表", description = "获取指定日期的所有可用时间段")
    public Result<List<TimeSlots>> getTimeSlotsByDate(
            @Parameter(description = "查询日期，格式：yyyy-MM-dd", required = true)
            @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
        
        log.info("获取日期 {} 的时间段列表", date);
        try {
            List<TimeSlots> timeSlots = timeSlotsService.getTimeSlotsByDate(date);
            return ResultUtil.success(timeSlots);
        } catch (Exception e) {
            log.error("获取时间段列表失败", e);
            return ResultUtil.fail("获取时间段列表失败");
        }
    }

    @GetMapping("/available-dates")
    @Operation(summary = "获取所有可用日期", description = "获取展馆开放的所有可预约日期")
    public Result<List<LocalDate>> getAvailableDates() {
        
        log.info("获取所有可用日期");
        
        try {
            List<LocalDate> dates = timeSlotsService.getAvailableDates();
            return ResultUtil.success(dates);
        } catch (Exception e) {
            log.error("获取可用日期失败", e);
            return ResultUtil.fail("获取可用日期失败");
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取时间段详情", description = "获取指定时间段的详细信息")
    public Result<TimeSlots> getTimeSlotById(
            @Parameter(description = "时间段ID", required = true)
            @PathVariable Long id) {
        
        log.info("获取时间段详情，ID: {}", id);
        
        try {
            TimeSlots timeSlot = timeSlotsService.getById(id);
            if (timeSlot == null) {
                return ResultUtil.fail("时间段不存在");
            }
            return ResultUtil.success(timeSlot);
        } catch (Exception e) {
            log.error("获取时间段详情失败", e);
            return ResultUtil.fail("获取时间段详情失败");
        }
    }
}
